package com.vinjogames.gfx;

import javax.microedition.khronos.opengles.GL10;

import com.vinjogames.util.Logger;

import android.graphics.PointF;

public class GUI {
	private PointF dStickLoc;
	private static PointF moveLoc;
	private static PointF moveStart;
	private float dStickRadius;
	private float dx, dy;
	
	private short state;
	protected final short STATE_WORLD = 0;
	protected final short STATE_BATTLE = 1;
	protected final short STATE_MENU = 2;
	
	public GUI() {
		state = STATE_BATTLE;
		moveLoc = new PointF(0, 0);
		moveStart = new PointF(0, 0);
		dStickRadius = 1;
		dStickLoc = new PointF(1,1);
		dx = dy = 0;
	}
	
	public void update(){
		dx = moveLoc.x > 1 ? 1 : moveLoc.x;
		dy = moveLoc.y > 1 ? 1 : moveLoc.y;
	}

	public void draw(GL10 gl) {
		Shapes.drawCircle(gl, dStickRadius, dStickLoc, 32, 0, 0, 1, 1);
		Shapes.drawArc(gl, dStickRadius, dStickLoc.x, dStickLoc.y, 32, 45, 315, false, 1, 0, 0, 1);
		Shapes.drawArc(gl, dStickRadius, dStickLoc.x, dStickLoc.y, 32, 135, 225, false, 0, 0, 1, 1);
		gl.glPushMatrix();
		gl.glTranslatef(dx, dy, 0);
		Shapes.drawCircle(gl, .5f, dStickLoc, 32, 0, 1, 0, 1);
		
		PointF fpsLoc = new PointF(-((Camera.GL_WIDTH/2f)-.5f),(Camera.GL_HEIGHT /2) -.5f);
		Number.drawNumber(GameRenderer.getFPS(), gl,fpsLoc , .1f);
		
		gl.glPopMatrix();
		
		switch(state){
		case STATE_WORLD:
			break;
		case STATE_BATTLE:
			Shapes.drawCircle(gl, .5f, 9.25f, .75f, 32, 1, 0, 0, 1);
			Shapes.drawCircle(gl, .5f, 8, .75f, 32, 0, 0, 1, 1);
			Font.drawString(gl, "Att", new PointF(9.25f, .75f), .5f);
			Font.drawString(gl, "Sp", new PointF(8f, .75f), .5f);
			break;
		case STATE_MENU:
			break;
		}
	}
	
	public boolean handleTouch(PointF loc){
		if(Math.sqrt(loc.x * loc.x + loc.y * loc.y) < dStickRadius * 2){
			return true;
		}
		// Insert other checks for GUI here-ish.
		return false;
	}
	
	public PointF move(PointF loc) {
		if(moveStart.equals(0, 0)){
			moveStart.set((loc.x - dStickRadius), (loc.y - dStickRadius));
			moveLoc.set(moveStart);
			return moveStart;
		}
		else {
			return moveLoc;
		}
	}
	
	public PointF passiveMove() {
		return moveLoc;
	}
	
	public PointF handleMove(PointF loc) {
		if(moveLoc.equals(0,0))
			moveLoc.set((moveStart.x + loc.x), (moveStart.y + loc.y));
		else{
			moveLoc.set((moveLoc.x + loc.x), (moveLoc.y + loc.y));
		}
		return moveLoc;
	}
	
	public void handleUp(PointF loc) {
		moveLoc.set(0, 0);
		moveStart.set(0, 0);
	}
	
	public int calculateTouchMove(PointF loc) {
		if(Math.abs(loc.x) > Math.abs(loc.y)){
			if(Math.signum(loc.x) > 0)
				return 1;
			else return 3;
		}
		if(Math.signum(loc.y) > 0)
			return 2;
		return 4;
	}
}
